package com.formen.ndk;

import android.graphics.*;
import android.os.*;
import android.support.v4.app.*;
import android.util.*;
import android.view.*;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.*;
import android.widget.*;

import com.anim.*;
import com.friendview.*;
import com.listview.*;


public class Main1 extends FragmentActivity implements OnClickListener {

	/* slide menu */
	private DisplayMetrics metrics;
	private LinearLayout ll_mainLayout;
	private LinearLayout ll_menuLayout;
	private FrameLayout.LayoutParams leftMenuLayoutPrams;
	private int leftMenuWidth;
	private static boolean isLeftExpanded;
	private ImageView  btn1, btn2, btn3, btn4;
	private ImageView bt_left;
	
	
	private FrameLayout fraglayout;
	
	int mCurrentFragmentIndex;
	public final static int FRAGMENT_ONE = 0;
	public final static int FRAGMENT_TWO = 1;
	public final static int FRAGMENT_THREE = 2;
	public final static int FRAGMENT_FOUR = 3;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main1);
		
		mCurrentFragmentIndex = FRAGMENT_ONE;
		
		fraglayout = (FrameLayout)findViewById(R.id.fraglayout);
		
		fragmentReplace(0);
		initSildeMenu();
	}
	
	public void fragmentReplace(int reqNewFragmentIndex) {		
		/*
		 // Create new fragment and transaction
			Fragment newFragment = new OneFragment();
			FragmentTransaction transaction = getFragmentManager().beginTransaction();

			//effect
			transaction.setCustomAnimations(R.anim.fade_in,R.anim.fade_out);
			
			// Replace whatever is in the fragment_container view with this fragment,
			// and add the transaction to the back stack
			transaction.replace(R.id.p3, newFragment);
			
			// Setting Depth
			//transaction.addToBackStack(null);
			
			//transaction.remove(this);
			
			// Commit the transaction
			transaction.commit();
		 */
		
		Fragment newFragment = null;
		newFragment = getFragment(reqNewFragmentIndex);

		// replace fragment
		final FragmentTransaction transaction = getSupportFragmentManager()
				.beginTransaction();

		transaction.setCustomAnimations(R.anim.fade_in,R.anim.fade_out);
		
		transaction.replace(R.id.ll_fragment, newFragment);

		// Commit the transaction
		transaction.commit();

	}
	
	private Fragment getFragment(int idx) {
		Fragment newFragment = null;

		switch (idx) {
		case FRAGMENT_ONE:
			fraglayout.setBackgroundColor(Color.WHITE);
			newFragment = new NFCTest();
			break;
		case FRAGMENT_TWO:
			fraglayout.setBackgroundColor(Color.WHITE);
			newFragment = new FriendListView();
			break;
		case FRAGMENT_THREE:
			fraglayout.setBackgroundColor(Color.WHITE);
			newFragment = new SnsListView();
			break;
		case FRAGMENT_FOUR:
			fraglayout.setBackgroundColor(Color.WHITE);
			newFragment = new Profile();
			break;
		default:
			//Log.d(TAG, "Unhandle case");
			break;
		}
		
		return newFragment;
	}
	
	private void initSildeMenu() {

		// init left menu width
		metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		leftMenuWidth = (int) ((metrics.widthPixels) * 0.25);

		// init main view
		ll_mainLayout = (LinearLayout) findViewById(R.id.ll_mainlayout);

		// init left menu
		ll_menuLayout = (LinearLayout) findViewById(R.id.ll_menuLayout);
		leftMenuLayoutPrams = (FrameLayout.LayoutParams) ll_menuLayout
				.getLayoutParams();
		leftMenuLayoutPrams.width = leftMenuWidth;
		ll_menuLayout.setLayoutParams(leftMenuLayoutPrams);

		// init ui
		bt_left = (ImageView) findViewById(R.id.header_bt1);
		bt_left.setOnClickListener(this);

		btn1 = (ImageView) findViewById(R.id.btn1);
		btn2 = (ImageView) findViewById(R.id.btn2);
		btn3 = (ImageView) findViewById(R.id.btn3);
		btn4 = (ImageView) findViewById(R.id.btn4);
		btn1.setOnClickListener(this);
		btn2.setOnClickListener(this);
		btn3.setOnClickListener(this);
		btn4.setOnClickListener(this);
	}

	/**
	 * left menu toggle
	 */
	private void menuLeftSlideAnimationToggle() {

		if (!isLeftExpanded) {

			isLeftExpanded = true;

			// Expand
			new OpenAnimation(ll_mainLayout, leftMenuWidth,
					Animation.RELATIVE_TO_SELF, 0.0f,
					Animation.RELATIVE_TO_SELF, 0.25f, 0, 0.0f, 0, 0.0f);

			// disable all of main view
			FrameLayout viewGroup = (FrameLayout) findViewById(R.id.ll_fragment)
					.getParent();
			enableDisableViewGroup(viewGroup, false);
	
			// enable empty view
			((LinearLayout) findViewById(R.id.ll_empty))
					.setVisibility(View.VISIBLE);

			findViewById(R.id.ll_empty).setEnabled(true);
			findViewById(R.id.ll_empty).setOnTouchListener(
					new OnTouchListener() {

						public boolean onTouch(View arg0, MotionEvent arg1) {
							menuLeftSlideAnimationToggle();
							return true;
						}
					});

		} else {
			isLeftExpanded = false;

			// close
			new CloseAnimation(ll_mainLayout, leftMenuWidth,
					TranslateAnimation.RELATIVE_TO_SELF, 0.25f,
					TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f, 0, 0.0f);

			// enable all of main view
			FrameLayout viewGroup = (FrameLayout) findViewById(R.id.ll_fragment)
					.getParent();
			enableDisableViewGroup(viewGroup, true);

			// disable empty view
			((LinearLayout) findViewById(R.id.ll_empty))
					.setVisibility(View.GONE);
			findViewById(R.id.ll_empty).setEnabled(false);
			
			viewGroup.invalidate();
		}
	}


	public static void enableDisableViewGroup(ViewGroup viewGroup,
			boolean enabled) {
		int childCount = viewGroup.getChildCount();
		for (int i = 0; i < childCount; i++) {
			View view = viewGroup.getChildAt(i);
			if (view.getId() != R.id.header_bt1) {
				view.setEnabled(enabled);
				if (view instanceof ViewGroup) {
					enableDisableViewGroup((ViewGroup) view, enabled);
				}
			}
		}
	}

	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.header_bt1:
			menuLeftSlideAnimationToggle();
			break;
		case R.id.btn1:
			fragmentReplace(0);
			break;
		case R.id.btn2:
			fragmentReplace(1);
			break;
		case R.id.btn3:
			fragmentReplace(2);
			break;
		case R.id.btn4:
			fragmentReplace(3);
			break;

		}

	}
	
	// ------------------------------- 뒤로 버튼 두번 누르면 종료되는 부분-----------------
	 	int check = 0;
	 	long backtime;

	 	public boolean onKeyDown(int key, KeyEvent event) {

	 		if ((key == KeyEvent.KEYCODE_BACK)) {

	 			long currentTime = System.currentTimeMillis();
	 			final int duration = 2000;
	 			check++;
	 			if (check == 1) {
	 				backtime = System.currentTimeMillis();
	 				Toast t = Toast
	 						.makeText(getBaseContext(),
	 								"프로그램을 종료하려면 \n'뒤로' 버튼을  한번 더 누르세요.",
	 								Toast.LENGTH_SHORT);
	 				t.setDuration(duration);
	 				t.show();

	 				new Thread(new Runnable() {

	 					public void run() {
	 						try {
	 							Thread.sleep(duration);
	 						} catch (InterruptedException e) {
	 							e.printStackTrace();
	 						}
	 						check = 0;
	 					}
	 				}).start();
	 			} else if (check == 2) {
	 				if (currentTime - backtime <= duration) {
	 					// return super.onKeyDown(key, event);
	 					android.os.Process.killProcess(android.os.Process.myPid());
	 				}
	 				check = 0;
	 			}
	 			return true;
	 		}
	 		return super.onKeyDown(key, event);
	 	}
	 // ------------------------------- 뒤로 버튼 두번 누르면 종료되는 부분-----------------

}
